<?php
/**
 * App/Alipay
 * User: Do
 * Date: 2018/5/21
 * Time: 10:24
 */
namespace alipay;
require_once "AopSdk.php";
class Alipay{

    protected $aop;

    public function __construct()
    {

        $this->aop = new \AopClient;
        $this->aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $this->aop->appId = config('alipay_config.app_id');
        $this->aop->rsaPrivateKey = config('alipay_config.rsa_private_key');
        $this->aop->format = "json";
        $this->aop->charset = "UTF-8";
        $this->aop->signType = "RSA2";
        $this->aop->alipayrsaPublicKey = config('alipay_config.open_public_key');

    }

    public function pay($subject,$out_trade_no,$total_amount,$passback_params=null)
    {

        $passback_params = urlencode($passback_params);
        $request = new \AlipayTradeAppPayRequest();
        $bizcontent = "{\"body\":\"我是测试数据\","
            . "\"subject\": \"$subject\","
            . "\"out_trade_no\": \"$out_trade_no\","
            . "\"timeout_express\": \"30m\","
            . "\"total_amount\": \"$total_amount\","
            . "\"passback_params\":\"$passback_params\","
            . "\"product_code\":\"QUICK_MSECURITY_PAY\""
            . "}";
        //$request->setNotifyUrl(config('alipay.alipay_config.notify_url'));
        $request->setNotifyUrl(config('alipay_config.notifyurl'));
        $request->setBizContent($bizcontent);
        $response = $this->aop->sdkExecute($request);
        //return htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
        return $response;

    }

    //退款
    public function refund($out_trade_no,$price)
    {

        $request = new \AlipayTradeRefundRequest ();
        $request->setBizContent("{" .
            "\"out_trade_no\":\"$out_trade_no\"," .
            "\"trade_no\":\"\"," .
            "\"refund_amount\":$price," .
            "\"refund_reason\":\"正常退款\"," .
            "\"out_request_no\":\"\"," .
            "\"operator_id\":\"\"," .
            "\"store_id\":\"\"," .
            "\"terminal_id\":\"\"" .
            "  }");

        $result = $this->aop->execute ( $request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){

            return json_encode($result);

        } else {

            return json_encode($result);

        }

    }


    //退款查询
    public function refundQurey($out_trade_no)
    {

        $request = new \AlipayTradeFastpayRefundQueryRequest ();
        $request->setBizContent("{" .
            "\"trade_no\":\"\"," .
            "\"out_trade_no\":\"$out_trade_no\"," .
            "\"out_request_no\":\"123\"" .
            "  }");
        $result = $this->aop->execute ( $request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){

            return json_encode($result);

        } else {

            return json_encode($result);

        }

    }


    //订单查询
    public function orderQuery($out_trade_no)
    {

        $request = new \AlipayTradeQueryRequest ();
        $request->setBizContent("{" .
            "\"out_trade_no\":\"$out_trade_no\"," .
            "\"trade_no\":\"\"" .
            "  }");
        $result = $this->aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            return json_encode($result);
        } else {
            return json_encode($result);
        }

    }

    //转账接口
    public function fundTransToAccount($out_biz_no,$payee_account,$amount)
    {

        $request = new \AlipayFundTransToaccountTransferRequest ();
        $request->setBizContent("{" .
            "\"out_biz_no\":\"$out_biz_no\"," .
            "\"payee_type\":\"ALIPAY_LOGONID\"," .
            "\"payee_account\":\"$payee_account\"," .
            "\"amount\":\"$amount\"," .
            "\"remark\":\"红豆恋提现转账\"" .
            "}");
        $result = $this->aop->execute ( $request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){

            return json_encode($result->alipay_fund_trans_toaccount_transfer_response);

        } else {

            return json_encode($result->alipay_fund_trans_toaccount_transfer_response);

        }

    }

    //异步回调
    public function notify_url($request)
    {
        $result = $this->aop->rsaCheckV1($request->post(),$this->aop->alipayrsaPublicKey);

        if($result){

            //file_put_contents('./malipay.txt',json_encode([$_POST,$_POST['trade_status']]));

            if($_POST['trade_status'] == 'TRADE_SUCCESS'){

                return ['out_trade_no'=>$_POST['out_trade_no'],'total_amount'=>$_POST['total_amount']];

            }else{

                return false;

            }

        }else{

            return false;
        }

    }


}